package com.study.controller;

import com.study.pojo.Dept;
import com.study.pojo.Result;
import com.study.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {
    @Autowired
    DeptService deptService;

    @GetMapping
    public Result list() {
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }

    //当定义形参名称和前端参数名称一致时，不需要加RequestParam注解
    //删除部门
    @DeleteMapping
    public Result delete(Integer id) {
        log.info("删除的部门id是"+id);
        deptService.deleteById(id);
        return Result.success();
    }

    //新增部门
    @PostMapping
    public Result add(@RequestBody Dept dept) {
//        System.out.println("新增部门Dept" + dept);
        log.info("新增部门是"+dept);
        deptService.add(dept);
        return Result.success();
    }

    //查询回显
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id) {
//        System.out.println("查询部门id:" + id);
        log.info("查询部门id是"+id);
        Dept dept = deptService.getInfo(id);
        return Result.success(dept);
    }
    //修改部门
    @PutMapping
    public Result update(@RequestBody Dept dept){
//        System.out.println("修改的部门："+dept);
        log.info("修改的部门是"+dept);
        deptService.update(dept);
        return Result.success();
    }
}
